/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blink.model;

import java.io.PrintWriter;

/**
 * 
 * @author jerome
 */
public class ExperimentRenderer {

	public static char SEP = '\t';

	private static PrintWriter out;

	public ExperimentRenderer(PrintWriter out) {
		ExperimentRenderer.out = out;
	}

	public static void writeFile(StringBuffer buf) {
		try {
			out.println(buf);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeFile() {
		try {
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static StringBuffer renderExperiment(Experiment e) {
		StringBuffer sb = new StringBuffer();
		sb.append(e.getSubjectName());
		sb.append(SEP);
		sb.append(e.getSubjectLastname());
		sb.append(SEP);
		sb.append(e.getAge());
		sb.append('\n');
		sb.append(e.getComment());

		for (Task t : e.getTasks()) {
			sb.append('\n');
			renderTask(t, sb);
		}

		return sb;
	}

	public static StringBuffer renderTrial(Trial t) {
		StringBuffer sb = new StringBuffer();
		sb.append(t.getTask().getName());
		sb.append(SEP);
		for (int i = 0; i < t.getUserResponses().length; i++) {
			sb.append(t.getUserResponses()[i]);
			sb.append(SEP);
			sb.append(t.getCorrectResponses()[i]);
			sb.append(SEP);
		}
		sb.append('\n');

		return sb;
	}

	private static void renderTask(Task t, StringBuffer sb) {
		sb.append(t.getName());
		sb.append(SEP);
		sb.append(t.getDisplayTime());
		sb.append(SEP);
		sb.append(t.getLagTime());
		sb.append(SEP);
		sb.append(t.getMinGapBetweenFillers());
		sb.append('\n');
	}
}
